package com.kbtd.interceptor;

import com.kbtd.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 登录权限过滤器（拦截需要登录的请求，判断是否登录，对已登录用户放行，未登录用户拦截）
 *
 * @author wp
 * @version 1.0
 */
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        if (ObjectUtils.isEmpty(UserHolder.getUser())) {
            log.info(request.getRequestURI() + "被拦截");
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
